import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
import React from 'react';
import { useRequest } from 'umi';
import { AuthClientManageAPI } from '../common/type';

const baseUrl = getBaseUrl();

/**
 * 认证客户端管理业务逻辑
 * @param curRes
 */
const AuthClientManageService = (curRes: Record<string, any>) => {
  /** 表格数据请求处理业务 */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (requestParams: AuthClientManageAPI.ITableRequestType) => {
      const dataGridURL = `${baseUrl}/clientController/dataGrid`;
      let postParams = {
        current: 1,
        pageSize: 10,
        sort: 'id',
        order: 'desc',
        ...requestParams,
      };
      // const searchParams: Record<string, any> = commonManageSearchParams(
      //   getSearchConfig,
      //   postParams,
      //   'sys_client',
      //   {
      //     // dictionary: `${ISearchTranslateAll.ASSETHAND_TRANS}`,
      //     organize: `${ISearchTranslateAll.MODIFY_DEPT}`,
      //     // area: `${ISearchTranslateAll.AREA_TRANS}`,
      //   },
      // )
      return requestFunComponFiex('POST', dataGridURL, postParams);
    },
    {
      manual: false,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /** 重新请求数据 */
  const refreshRequest = React.useCallback(
    (requestParams: AuthClientManageAPI.ITableRequestType | any) => {
      run({
        ...requestParams,
      });
    },
    [],
  );

  /** 管理页面数据及分页信息 */
  const authClientDataInfo = React.useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    refreshRequest,
    reloadRefreshRequest: refresh,
    ...authClientDataInfo,
  };
};

/** 管理页面上下文 */
const useContextInfo: AuthClientManageAPI.IManageContextType = {
  dataSource: [],
  loading: false,
  refreshRequest: ({}) => {},
  dicMapList: [],
  pagination: {},
  requestParams: {},
};

const AuthClientManageContext = React.createContext(useContextInfo);

export { AuthClientManageService, AuthClientManageContext };
